//
//  LAGBaseMapViewController.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2017/4/8.
//  Copyright © 2017年 2Bulu. All rights reserved.
//

#import "LAGBaseMapViewController.h"
#import <MapGisBusiness/LAMapSourceManager.h>
#import <MapBasics/MapBasics.h>
#import <MapGisBusiness/TrackPolyline.h>


@interface LAGBaseMapViewController ()

@end

@implementation LAGBaseMapViewController
{
    __weak MKTileOverlay *_srcLayer;
    
    UIButton *_switchBtn;
    CLLocationManager *_lm;
    
#warning test
    id<LAGAnnotationExt> _debugAnno;
    id<LAOverLayExt> _debugLay;
    NSMutableArray<id<LAGAnnotationExt>> *_debugAnnotations;
}


-(nonnull instancetype)initWithAnnotation:(nullable id<LAGAnnotationExt>)ann
{
    self = [super init];
    if(self)
    {
        _debugAnno = ann;
    }
    return self;
}
-(nonnull instancetype)initWithOverlay:(nullable id<LAOverLayExt>)lay
{
    self = [super init];
    if(self)
    {
        _debugLay = lay;
    }
    return self;
}

-(void)addAnnotations:(nullable NSArray<id<LAGAnnotationExt>> *)ann
{
    if(!_debugAnnotations)
        _debugAnnotations = [NSMutableArray arrayWithCapacity:ann.count];
    
    [_debugAnnotations addObjectsFromArray:ann];
    if(self.isViewLoaded)
    {
        [_mapView addAnnotations:_debugAnnotations toClusterGroup:@"123"];
    }
}


-(void)testMethod
{
    if(_debugAnno)
    {
        [_mapView addAnnotation:_debugAnno];
        [_mapView setCenterCoordinate:_debugAnno.coordinate];
    }
    
    if(_debugLay)
    {
        //[_mapView addOverlay:_debugLay];
        [_mapView addOverlays:@[_debugLay]];
        [_mapView setRegion:MKCoordinateRegionForMapRect(_debugLay.boundingMapRect)];
    }
    
}

#pragma mark- lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];

    _mapView = [[LAGAppMapView alloc]initWithFrame:self.view.bounds];
    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;
    _mapView.showsScale = YES;
    _mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_mapView];
    
    
    _switchBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 84, self.view.frame.size.width, 64)];
    _switchBtn.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    [_switchBtn setTitle:@"选择图源" forState:UIControlStateNormal];
    [_switchBtn addTarget:self action:@selector(switchMapSourceClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_switchBtn];
    
    //debug
    UIButton *btnTest1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTest1.frame = CGRectMake(10, 160, 72, 24);
    btnTest1.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
    [btnTest1 setTitle:@"doTest1" forState:UIControlStateNormal];
    [btnTest1 addTarget:self action:@selector(doTestWork1:) forControlEvents:UIControlEventTouchUpInside];
    [_mapView addSubview:btnTest1];
    
    UIButton *btnTest2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTest2.frame = CGRectMake(btnTest1.frame.size.width+btnTest1.frame.origin.x+10, btnTest1.frame.origin.y, btnTest1.frame.size.width, btnTest1.frame.size.height);
    btnTest2.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
    [btnTest2 setTitle:@"doTest2" forState:UIControlStateNormal];
    [btnTest2 addTarget:self action:@selector(doTestWork2:) forControlEvents:UIControlEventTouchUpInside];
    [_mapView addSubview:btnTest2];
    
    
    
    [self switchToMapSourceAtIndex:0];
    MKCoordinateRegion r = {{22.668262,113.971483},{0.382062,0.257492}};
//    [_mapView setRegion:r];
    
    [self testMethod];
    
    UIBarButtonItem *bitm = [[UIBarButtonItem alloc]initWithTitle:@"test" style:UIBarButtonItemStyleDone target:self action:@selector(reverseSwitch:)];
    self.navigationItem.rightBarButtonItem = bitm;
    
    [_mapView addAnnotations:_debugAnnotations toClusterGroup:@"123"];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

-(IBAction)reverseSwitch:(UIBarButtonItem *)sender
{
    if([_debugLay isKindOfClass:[TrackPolyline class]])
    {
        TrackPolyline *l = _debugLay;
        l.reverse = !l.reverse;
        [l refreshPolyLine];
    }
}

#pragma mark- test
-(IBAction)doTestWork1:(UIButton *)sender
{
#warning test
    
    LAGBaseMapViewController *vc = [[LAGBaseMapViewController alloc]initWithOverlay:_debugLay];
    [vc addAnnotations:_debugAnnotations];
    vc.title = @"9+";
    [self.navigationController pushViewController:vc animated:YES];
    return;
    MKCoordinateRegion r = _mapView.region;
    CLLocationCoordinate2D orgCord = CLLocationCoordinate2DMake(r.center.latitude-r.span.latitudeDelta/2, r.center.longitude-r.span.longitudeDelta/2);
    
    //进一步缩小添加区域
    int divide = 1;
    orgCord.latitude = orgCord.latitude + r.span.latitudeDelta*(1-1/divide)/2;
    orgCord.longitude = orgCord.longitude + r.span.longitudeDelta*(1-1/divide)/2;
    r.span.latitudeDelta /= divide;
    r.span.longitudeDelta /= divide;
    _mapView.annotationClusteringRadium = 60;

    NSLog(@"---start to add");
    
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:10000];
    
    int latDiv = 1000;
    int lngDiv = 1500;
    for(int i=0; i<5000; i++)
    {
        double lat = orgCord.latitude +  (r.span.latitudeDelta/latDiv) * (rand()%latDiv);
        double lng = orgCord.longitude + (r.span.longitudeDelta/lngDiv) * (rand()%lngDiv);
        
//        TestAnnotation *a = [[TestAnnotation alloc]init];
//        a.coordinate = CLLocationCoordinate2DMake(lat, lng);
//        [mArray addObject:a];
    }
    
    static int tag = 0;
    
    //[_mapView addAnnotations:mArray toClusterGroup:[NSString stringWithFormat:@"%d",(tag++)%2]];
    [_mapView addAnnotations:mArray toClusterGroup:@"ad"];
    
    NSLog(@"---FINISHED---------");
}


-(IBAction)doTestWork2:(UIButton *)btn
{
//    for(TestAnnotation *ann in _debugAnnotations)
//    {
//        CLLocationCoordinate2D cord = ann.coordinate;
//        cord.longitude -= 0.002;
//        cord.latitude += 0.003;
//        ann.coordinate = cord;
//    }
}

#pragma mark- getter/setter

#pragma mark-

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{

}

-(NSArray *)mapSources
{
    NSArray *maps = [LAMapSourceManager  allMapSources];
    return maps;
}

-(IBAction)switchMapSourceClicked:(id)sender
{
    UIActionSheet *as = [[UIActionSheet alloc]initWithTitle:@"选择图源" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    
    NSArray *maps = [self mapSources];
    for(LAMapSource *src in maps)
    {
        [as addButtonWithTitle:src.name];
    }
    
    [as showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex != actionSheet.cancelButtonIndex)
    {
        [self switchToMapSourceAtIndex:buttonIndex - 1];
    }
}

-(void)switchToMapSourceAtIndex:(NSUInteger)idx
{
    NSArray *srces = [self mapSources];
    if(idx < srces.count)
    {
        LAMapSource *src = [srces objectAtIndex:idx];
        [self switchToMapSource:src];
    }
}
-(void)switchToMapSource:(LAMapSource *)src
{
    _mapView.contentSource = src;
}

@end
